<template>
    <el-dialog
        v-dialogDrag
        :title="!dataForm.id ? '新增' : '修改'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        @close="handleClose"
        :center="true"
    >
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
            <el-form-item label="标题">
                <el-input v-model="dataForm.title" placeholder="请输入标题" clearable></el-input>
            </el-form-item>
            <el-form-item label="总面积">
                <el-input
                    v-model="dataForm.totalArea"
                    placeholder="请输入总面积"
                    oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model="dataForm.address" placeholder="请输入地址" clearable></el-input>
            </el-form-item>
            <el-form-item label="种植类型">
                <el-input v-model="dataForm.type" placeholder="请输入种植类型" clearable></el-input>
            </el-form-item>
            <el-form-item label="共享期限">
                <el-input v-model="dataForm.deadline" placeholder="请输入共享期限" clearable></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';
export default {
    data() {
        return {
            typeOptions: [],
            visible: false,
            dataForm: {
                title: '',
                totalArea: '',
                address: '',
                type: '',
                deadline: ''
            },
            visible: false,
            dialogType: 1
        };
    },
    methods: {
        handleClose() {
            this.dataForm = {
                title: '',
                totalArea: '',
                address: '',
                type: '',
                deadline: ''
            };
            this.$refs['dataForm'].resetFields();
        },
        init(item, type, appId) {
            console.log(type, item);
            this.visible = true;
            this.dialogType = type;
            if (item && type == 2) {
                this.dataForm = item;
            }
        },

        // 表单提交
        dataFormSubmit() {
            console.log(this.dataForm.users);
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    let params = cloneDeep(this.dataForm);
                    var tick = this.dialogType == 1 ? API.sharefarmland.addFarmland(params) : API.sharefarmland.editFarmland(params);
                    tick.then((data) => {
                        if (data.code == '0000') {
                            this.$message({
                                message: `${this.dialogType == 2 ? '修改成功' : '新增成功'}`,
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit('refreshDataList');
                                }
                            });
                        }
                    });
                }
            });
        }
    }
};
</script>